/**
 * Created by walkman on 2017/6/22.
 */
angular.module('WfbPlatformApp').controller('CartController', function ($rootScope, $scope, $http, $timeout) {
  $scope.$on('$viewContentLoaded', function () {
    // initialize core components
  });
  //不分页
  $http.post('/purchase/list')
    .then(
      function success(json) {
        var json = json.data;
        if (json.retCode == 1) {
          //数据渲染
          $scope.datalist = json.data;
        }
      },
      function error(json) {
        alert(json);
      }
    );
  //获取地址
  $http.post('/addr/list')
    .then(
      function success(json) {
        var json = json.data;
        if (json.retCode == 1) {
          //数据渲染
          var a = document.getElementById('createAddr');
          var select = document.getElementById('mailPostId');
          if (json.dataList.length == 0) {
            a.style.display = 'block';
            select.style.display = 'none';
          } else {
            a.style.display = 'none';
            select.style.display = 'block';
            $scope.addList = json.dataList;
          }
        }
      },
      function error(json) {
        alert(json);
      }
    );

  //剩余积分
  //  /transaction/getPoint
  $http.post('/transaction/getPoint')
    .then(
      function success(json) {
        var json = json.data;
        if (json.retCode == 1) {
          //数据渲染
          $scope.cpElecPoint = json.data.cpElecPoint;
        }
      },
      function error(json) {
        alert(json);
      }
    );

  //分页
  /*$scope.flag = true;
  $scope.pageSize = 1;
  $scope.selPage = 1;
  $scope.isActivePage = function (page) {
    //console.log('in....'+ page);
    return $scope.selPage == page;
  };
  var postUrl = '/purchase/list';
  post(postUrl,$scope.selPage,$scope.pageSize);

  function post(apiUrl,pageIdx,pageSize){
    var pageIdx = pageIdx;
    var pageSize = pageSize;
    $http.post(postUrl,{
      pageNo:pageIdx,
      pageSize:pageSize
    }).then(
      function success(json) {
        var json = json.data;
        if(json.retCode==1){
          //数据渲染
          $scope.datalist = json.data;
          //分页
          $scope.totalCount =  json.totalCount;
          $scope.pages = Math.ceil($scope.totalCount/$scope.pageSize); //分页数
          $scope.flag && firstLoad();
          //
          $scope.isActivePage($scope.selPage);
        }
      },
      function error(json) {
        alert(json);
      }
    );
    //jquery 的异步请求无法渲染数据，异步请求无法正确加载多页数时的页码样式 0_0
    /!*jQuery.ajax({
     url:apiUrl,
     type:'post',
     data:{
     pageNo:pageIdx,
     pageSize:pageSize
     },
     async:false,
     success:function (json) {
     if(json.retCode==1){
     //数据渲染
     $scope.datalist = json.data;
     //分页
     $scope.pages = Math.ceil(json.totalCount / $scope.pageSize); //分页数
     $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
     $scope.pageList = [];
     for (var i = 0; i < $scope.newPages; i++) {
     $scope.pageList.push(i + 1);
     }
     $scope.isActivePage(pageIdx);
     }else{
     alert('接口/purchase/list: '+json.retMsg);
     }
     }
     });*!/
  }
  function firstLoad() {
    //console.log( $scope.pages );
    $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
    $scope.pageList = [];
    for (var i = 0; i < $scope.newPages; i++) {
      $scope.pageList.push(i + 1);
    };
  }
  //打印当前选中页索引
  $scope.selectPage = function (page) {
    //不能小于1大于最大
    if (page < 1 || page > $scope.pages) return;
    $scope.flag = false;
    post(postUrl,page,$scope.pageSize);
    //最多显示分页数5
    if (page > 2) {
      //因为只显示5个页数，大于2页开始分页转换
      var newpageList = [];
      for (var i = (page - 3) ; i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)) ; i++) {
        newpageList.push(i + 1);
      }
      $scope.pageList = newpageList;
    }
    $scope.selPage = page;
  };
  //上一页
  $scope.Previous = function () {
    $scope.selectPage($scope.selPage - 1);
  };
  //下一页
  $scope.Next = function () {
    $scope.selectPage($scope.selPage + 1);
  };

  $scope.del_cart = function (e) {
    console.log( '......');
    // $('body').mLoading('show');
    //  $.ajax({
    //  url:'/goods/delChart',
    //  data:{
    //  purchaseid:$(e).parent().parent().attr('data-tid')
    //  },
    //  type:'post',
    //  success:function (json) {
    //  $('body').mLoading('hide');
    //  if(json.retCode==1){
    //  $(e).parent().parent().remove();
    //  }else{
    //  alert('删除失败。。');
    //  }
    //  }
    //  });
  }*/
});
